export const state = () => ({
    lineItems: null,
    orderId: -1
})

export const mutations = {
    setLineItems(state, lineItems) {
        state.lineItems = lineItems
    },
    setOrderId(state, orderId) {
        state.orderId = orderId
    }
}

export const actions = {
    addOrder({}, order) {
        return this.$api.order.addOrder(order).then((res) => {
            if(res.data.status === 'OK') {
                return Promise.resolve('OK')
            }
            else {
                return Promise.reject('error')
            }
        }, (error) => {
            return Promise.reject(error)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    checkPayment({}, orderId) {
        return this.$api.order.checkPayment(orderId).then((res) => {
            if(res.data.status === 'OK') {
                return Promise.resolve('OK')
            }
            else {
                return Promise.reject(res.data.msg)
            }
        }, (error) => {
            return Promise.reject(error)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    updateOrderStatus({}, orderStatus) {
        return this.$api.order.updateOrderStatus(orderStatus).then((res) => {
            if(res.data.status === 'OK') {
                return Promise.resolve('OK')
            }
            else {
                return Promise.reject(res.data.msg)
            }
        }, (error) => {
            return Promise.reject(error)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    getOrderListByUserID({}, username) {
        return this.$api.order.getOrderListByUserID(username).then((res) => {
            if(res.data.status === 'OK') {
                return Promise.resolve(res.data.data)
            }
            else {
                return Promise.reject(res.data.msg)
            }
        }, (error) => {
            return Promise.reject(error)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    getOrderByOrderId({}, orderId) {
        return this.$api.order.getOrderByOrderId(orderId).then((res) => {
            if(res.data.status === 'OK') {
                return Promise.resolve(res.data.data)
            }
            else {
                return Promise.reject(res.data.msg)
            }
        }, (error) => {
            return Promise.reject(error)
        }).catch((error) => {
            return Promise.reject(error)
        })
    }
}